home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / update~4.z / update~4 / lib_stdio_bitset.h < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-06  |  2.8 KB  |  112 lines

  1. /*            b i t s e t
  2.  * (C) Copyright C E Chew
  3.  *
  4.  * Feel free to copy, use and distribute this software provided:
  5.  *
  6.  *    1. you do not pretend that you wrote it
  7.  *    2. you leave this copyright notice intact.
  8.  *
  9.  *
  10.  * This implements a set of operators to manipulate bitsets
  11.  * is a machine independent way. It may be necessary for other
  12.  * machines to set up the constants at the front (such as the
  13.  * width of int, etc).
  14.  *
  15.  * To avoid double evaluation of the argument in some of the
  16.  * macros, define the data structure as follows:
  17.  *
  18.  *    1. the bitset is int []
  19.  *    2. the first int will be used as scratch int [0]
  20.  *    3. all the following elements will be used for the bitset proper
  21.  *
  22.  * Patchlevel 1.1
  23.  *
  24.  * Edit History:
  25.  * 05-Sep-1989    Change so that MINIX is not required to be defined.
  26.  */
  27.  
  28. #if    defined(BSD)
  29. # include <values.h>
  30. # define CHAR_BIT    (BITSPERBYTE)
  31.   extern void bzero    P((char *, int));
  32. # define _BITEMPTY(a,n)    (bzero((a), (n)))
  33. # define _BITFILL(a,n)  {char *p=(a); int c=(n); for ( ; c--; *p++ = ~0) ; }
  34. #endif
  35.  
  36. #if    !defined(CHAR_BIT)
  37. # include <limits.h>
  38. # include <memory.h>
  39. # define _BITEMPTY(a,n)    (memset((a), 0, (n)))
  40. # define _BITFILL(a,n)  (memset((a), ~0, (n)))
  41. #endif
  42.  
  43. /* Declare the relative sizing of the components */
  44.  
  45. #define _BITELEMENT    unsigned int
  46. #define _BITSINCHAR    CHAR_BIT
  47.  
  48. /* Determine how many bits there are in an elemental bitset */
  49.  
  50. #define _BITS        (((int)(sizeof(_BITELEMENT)))*_BITSINCHAR)
  51.  
  52. /* Declare the relative offsets of the pieces in the structure */
  53.  
  54. #define _BIT_SCRATCH_    0
  55. #define    _BIT_SET_    1
  56.  
  57. /* Define how to find a bit within a bitset */
  58.  
  59. #define _BITSIZE_(n)    (((n)+_BITS-1)/_BITS)
  60. #define _BITGROUP_(n)    ((n)/_BITS)
  61. #define _BITMASK_(n)    (1<<((n)%_BITS))
  62.  
  63. /* Define how to find a bit within the data structure */
  64.  
  65. #define _BITSIZE(n)    (_BITSIZE_(n)+_BIT_SET_)
  66. #define _BITGROUP(n)    (_BITGROUP_(n)+_BIT_SET_)
  67. #define _BITMASK(n)    (_BITMASK_(n))
  68.  
  69. /*
  70.  * Declare a bitset of length n bits
  71.  */
  72.  
  73. #define bitstring(name, n) _BITELEMENT name[_BITSIZE(n)]
  74.  
  75. /*
  76.  * Clear all elements of the bitset
  77.  */
  78.  
  79. #define bitempty(name, n) (_BITEMPTY(&name[_BIT_SET_], \
  80.                     _BITSIZE_(n)*((int)sizeof(_BITELEMENT))))
  81.  
  82. /*
  83.  * Set all elements of the bitset
  84.  */
  85.  
  86. #define bitfill(name, n) (_BITFILL(&name[_BIT_SET_], \
  87.                    _BITSIZE_(n)*((int)sizeof(_BITELEMENT))))
  88.  
  89. /*
  90.  * Set one bit in the bitset
  91.  */
  92.  
  93. #define bitset(name, n) (name[_BIT_SCRATCH_]=(n), \
  94.              name[_BITGROUP(name[_BIT_SCRATCH_])] |= \
  95.                _BITMASK(name[_BIT_SCRATCH_]))
  96.  
  97. /*
  98.  * Clear one bit in the bitset
  99.  */
  100.  
  101. #define bitclear(name, n) (name[_BIT_SCRATCH_]=(n), \
  102.                name[_BITGROUP(name[_BIT_SCRATCH_])] &= \
  103.                  ~_BITMASK(name[_BIT_SCRATCH_]))
  104.  
  105. /*
  106.  * Test one bit in the bitset
  107.  */
  108.  
  109. #define bittest(name, n) (name[_BIT_SCRATCH_]=(n), \
  110.               name[_BITGROUP(name[_BIT_SCRATCH_])] & \
  111.                 _BITMASK(name[_BIT_SCRATCH_]))
  112.